//use std::fs::{self, File};
//use std::io::prelude::{BufRead, Write};
//use std::io::BufReader;
//use std::process::Command;

//use regex::Regex;

fn main() {
    println!("cargo:rerun-if-changed=protobuf/message_id.toml");
    println!("cargo:rerun-if-changed=protobuf/hall.proto");
    println!("cargo:rerun-if-changed=protobuf/room.proto");
    println!("cargo:rerun-if-changed=protobuf/client.proto");

    let mut config = prost_build::Config::new();
    config
        .out_dir("src")
        .type_attribute(
            ".",
            "#[derive(honeybee::prelude::UserMessageId)]\n#[rustfmt::skip]",
        )
        .protoc_arg("--experimental_allow_proto3_optional")
        .compile_protos(
            &[
                "./protobuf/hall.proto",
                "./protobuf/room.proto",
                "./protobuf/client.proto",
            ],
            &["./protobuf/"],
        )
        .unwrap();

    //gen_proto_impl_msgid("../../x22_sproto/protobuf/name2id.txt");

    //check_and_move_file("game.rs", "src/protos");
    //check_and_move_file("pb_packet.rs", "src");
}

/*fn gen_proto_impl_msgid(file: &str) {
    let file = File::open(file).unwrap();
    let file_buffer = BufReader::new(file);
    let re = Regex::new(r"([[:alnum:]]+).([[:alnum:]]+) = (\d)").unwrap();
    let mut code = String::new();
    code += "// auto generated with name2id rule.";
    for line in file_buffer.lines() {
        let line = line.unwrap();
        for cap in re.captures_iter(&line) {
            code += "\n";
            code += &format!(
                "impl crate::net::MessageId for crate::protos::{}::{} {{\n",
                &cap[1], &cap[2]
            );
            code += &format!("    const PID: u16 = {};\n", &cap[3]);
            code += "}\n";
        }
    }

    let mut f = fs::File::create("pb_packet.rs").expect("create pb_packet.rs failed.");
    f.write_all(code.as_bytes())
        .expect("write pb_packet.rs failed.");
}*/

/*fn check_and_move_file(file: &str, dest_dir: &str) {
    let status = Command::new("diff")
        .arg(file)
        .arg(format!("{}/{}", dest_dir, file))
        .status()
        .expect("execute diff failed.");

    if !status.success() {
        Command::new("mv")
            .arg(file)
            .arg(dest_dir)
            .status()
            .expect("execute mv failed.");
    } else {
        Command::new("rm")
            .arg(file)
            .status()
            .expect("execute rm failed.");
    }
}*/
